/**
 * 实现Promise.all方法
 */
import { sleep } from "./sleep";

async function myAll<T extends unknown[] | []>(values: T): Promise<{ [P in keyof T]: Awaited<T[P]> }> {
  const result = [];
  for (let i = 0; i < values.length; i++) {
    result.push(await values[i]);
  }

  return result as { [P in keyof T]: Awaited<T[P]> };
}

// 测试
async function request(value: string) {
  await sleep(1000);
  return value;
}

async function mainAll() {
  console.log("start");
  const res = await myAll([request("aa"), request("bb"), request("cc")]);

  console.log(res);
}

mainAll();
